package net.oschina.esb.webmvc.context;

import net.oschina.esb.util.AssertUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;

/**
 * @author Arvin
 * @time 2018/3/15 19:56
 */
public class ContextHolder {

    private static final Log logger = LogFactory.getLog(ContextHolder.class);

    /** Spring Application Context */
    private static ApplicationContext acx;

    /** Servlet Context */
    private static ServletContext servletContext;

    private static Environment environment;

    public static ApplicationContext getAcx() {
        return acx;
    }

    public static ServletContext getServletContext() {
        return servletContext;
    }

    public static Environment getEnvironment() {
        return environment;
    }

    public static void init(ServletContext servletContext, Environment environment) {
        AssertUtil.assertNotNull(servletContext, "ContextHolder.init: servletContext should not be null");

        logger.info("Init ContextHolder......");

        ContextHolder.servletContext = servletContext;
        ContextHolder.acx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        ContextHolder.environment = environment;
    }


}
